package com.glory.good_dark.activity;

import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.good_dark.R;
import com.glory.good_dark.application.App;
import com.glory.good_dark.application.BaseActivity;
import com.glory.good_dark.entity.SystemConfigEntity;
import com.glory.good_dark.entity.UnlockEntity;
import com.glory.good_dark.entity.response.BaseEntity;
import com.glory.good_dark.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class SystemConfigActivity extends BaseActivity {

    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.EQPID_Txt)
    TextView EQPIDTxt;
    @BindView(R.id.tvLockType)
    TextView tvLockType;
    @BindView(R.id.tvDevice)
    TextView tvDevice;
    @BindView(R.id.ivLockTypeListArrow)
    ImageView ivLockTypeListArrow;
    @BindView(R.id.flLockTypeList)
    FrameLayout flLockTypeList;
    @BindView(R.id.etPort)
    EditText etPort;
    @BindView(R.id.etIP)
    EditText etIP;
    private String mEqpid;
    private List<String> mTypeList;
    private FreeUI_GeneralPop lockTypePop;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_system_config;
    }

    @Override
    protected void initView() {
        EQPIDTxt.setText(App.EQPID);
        mTypeList = new ArrayList<>();
    }

    @Override
    protected void initData() {
        FreeUI_EntityCallBack<BaseEntity<SystemConfigEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseEntity<SystemConfigEntity>>
                (new TypeToken<BaseEntity<SystemConfigEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess
                    (final Response<BaseEntity<SystemConfigEntity>> response) {
                if (response.body().isSuccess(getApplicationContext(),getSupportFragmentManager())) {
                    SystemConfigEntity body = response.body().getData().getBody();
                    etPort.setText(body.getPORT());
                    etIP.setText(body.getIPADRESS());
                    tvDevice.setText(body.getDEVICEID());
                    List<SystemConfigEntity.SystemType> connectionmode = body.getCONNECTIONMODE();
                    if (connectionmode.size() >= 0){
                        for (SystemConfigEntity.SystemType type : connectionmode){
                            mTypeList.add(type.getMODE());
                        }
                    }
                }
            }

            @Override
            public void onError
                    (Response<BaseEntity<SystemConfigEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "PDAGetEqpConfigInfo");
            }
        };
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("EQP_ID", App.EQPID);
        jsonObject.addProperty("OPERATOR", App.userEntity.getWORKNO());
        NetUtils.requestPostNet(App.WEB_URL + "/api/PDA", this, callBack, "PDAGetEqpConfigInfo", jsonObject);
    }

    @OnClick(R.id.flLockTypeList)
    public void onViewClicked() {
        if (mTypeList.size() <= 0) {
            showToast("未获取到连接模式列表", false);
            return;
        }

        if (lockTypePop != null) {
            if (lockTypePop.isShowing()) {
                lockTypePop.dismiss();
            }
        } else {
            lockTypePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flLockTypeList.getWidth(), mTypeList, ivLockTypeListArrow, (code, i) -> {
                tvLockType.setText(code);
            });
        }
        FreeApi_PopUtils.setArrowShow(ivLockTypeListArrow);
        lockTypePop.showAsDropDown(flLockTypeList, 0, 0);

    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("系统配置");
        freeUI_titleView.setRightListener("提交", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (etIP.getText().toString().equals("")){
                    showToast("IP地址不能为空",false);
                }else if (etPort.getText().equals("")){
                    showToast("端口号不能为空",false);
                }else if (tvLockType.getText().toString().equals("")){
                    showToast("请选择连接模模式",false);
                }else {
                    FreeUI_EntityCallBack<BaseEntity<SystemConfigEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseEntity<SystemConfigEntity>>
                            (new TypeToken<BaseEntity<SystemConfigEntity>>() {
                            }.getType(), getSupportFragmentManager(), this) {

                        @Override
                        public void onSuccess
                                (final Response<BaseEntity<SystemConfigEntity>> response) {
                            if (response.body().isSuccess(getApplicationContext(),getSupportFragmentManager())) {
                                showToast("修改成功",true);
                                new Handler().postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        SystemConfigActivity.this.finish();
                                    }
                                },2500);
                            }
                        }

                        @Override
                        public void onError
                                (Response<BaseEntity<SystemConfigEntity>> response) {
                            super.onError(response);
                            loadError(response.getException(), "PDASubmitEqpConfigInfo");
                        }
                    };
                    JsonObject jsonObject = new JsonObject();
                    jsonObject.addProperty("EQP_ID", App.EQPID);
                    jsonObject.addProperty("OPERATOR", App.userEntity.getWORKNO());
                    jsonObject.addProperty("IPADRESS", etIP.getText().toString());
                    jsonObject.addProperty("PORT", etPort.getText().toString());
                    jsonObject.addProperty("DEVICEID", tvDevice.getText().toString());
                    jsonObject.addProperty("CONNECTIONMODE", tvLockType.getText().toString());
                    NetUtils.requestPostNet(App.WEB_URL + "/api/PDA", this, callBack, "PDASubmitEqpConfigInfo", jsonObject);
                }
            }
        });
        return new FreeUI_AddViewEntity(getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY), llMain);
    }
}